; ORIGINAL: 3dt2.1/examples/rotate.scm
; $Id: rotate.scm,v 1.4 1997/04/30 20:24:17 jread Exp $
;- rotate.scm -----------------------------------------------------
; Rotate the view around the
; x axis (button 1), y-axis (button 2), or z-axis (button 3)
; (rotate #t) turns on rotation. and (rotate #f) turns off rotation.
;------------------------------------------------------------------
(require 'old-rb "old-rb.scm")

(define ang-step .1)
(define rotate
  (lambda (onoff)
    (let* ((axis #f)
          (orig-view #f)
          (prev-event #f)
          (button 0))
      (if onoff
        ;; TRUE: Define mouse hooks for tracking position
        (begin
          ;; When button pressed, save original view info & start tracking
          (set! mouse-down-hook
            (lambda (pe)
              (begin
                (set! orig-view (event:view pe))
                (set! button (event:button pe))
                (set! prev-event pe)
                ;; Determine rotation axis based on mouse button pressed
                (cond
                  ((= button 1)
                    (set! axis (gvector:cross (view:up orig-view)
                                              (gvector:from-to
                                                  (view:target orig-view)
                                                  (view:eye orig-view)))))
                  ((= button 2)
                    (set! axis (view:up orig-view)))
                  (else
                    (set! axis (gvector:from-to (view:target orig-view)
                                                (view:eye orig-view))))
                )
                (dl:dynamic-silhouette-display #f)
                (rb:scheme #t)
              )
            )
          )
          ;; As mouse moves, update view and refresh
          (set! rb-update-hook
            (lambda (pe)
              (if orig-view
                (let ((angle (* ang-step (if (= button 2)
                                           (- (event:x pe) (event:x prev-event))
                                           (- (event:y pe) (event:y prev-event))
                                         ))))
                  (view:set-eye (position:transform (view:eye orig-view)
                      (transform:rotation (view:target orig-view) axis angle))
					  orig-view)
                  (view:set-up (gvector:transform (view:up orig-view)
                      (transform:rotation (position 0 0 0) axis angle))
					  orig-view)
                  (view:refresh orig-view)
                  (set! prev-event pe)
                )
                #f
              )
            )
          )
          ;; When button released, stop tracking
          (set! mouse-up-hook
            (lambda (pe)
              (rb:scheme #f)
              (dl:dynamic-silhouette-display #t)
              (view:refresh orig-view)
              (set! orig-view #f)
            )
          )
        )
        ;; FALSE: Turn of tracking capability
        (begin
          (set! mouse-down-hook '())
          (set! mouse-up-hook '())
          (set! rb-update-hook '())
        )
      )
      ;; Return the new setting
      onoff
    )
  )
)
